package com.info33.platform.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.system.dto.SysConfigDTO;
import com.info33.platform.system.entity.SysConfig;
import com.info33.platform.system.service.SysConfigService;
import com.info33.platform.system.vo.SysConfigVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author admin
 */
@RestController
@RequestMapping("/SysConfig")
@Api(tags = "系统 - 配置")
public class SysConfigController extends BaseController<SysConfigService, SysConfig> {

    private final SysConfigService sysConfigService;

    @Autowired
    public SysConfigController(SysConfigService sysConfigService) {
        super(sysConfigService);
        this.sysConfigService = sysConfigService;
    }

    @GetMapping(value = "/pc/list/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 分页", notes = "可选入参完全匹配查询，同时匹配已经登录用户的租户过虑")
    public Result<IPage<SysConfigVO>> pcPage(
            @PathVariable int pageNum,
            @PathVariable int pageSize) {
        return Result.ok(sysConfigService.pcPage(new Page<>(pageNum, pageSize), getCurrentUser()));
    }

    @PostMapping(value = "/pc/one/config")
    @ApiOperation(value = "获取某项配置的对象", notes = "仅查询一条、无租户判断")
    public Result<SysConfig> pcConfig(
            @RequestBody SysConfigDTO dto) {
        return Result.ok(sysConfigService.getOne(new QueryWrapper<SysConfig>()
                .eq("config_type", dto.getConfigType())
                .last(TableConstant.LIMIT_ONE)));
    }

}